<?php
class mysql_db_driver {

    var $db;

    public function connect($host,$user,$pass) {
        $this->db = mysql_connect($host,$user,$pass);
        if(!$this->db) {
            return FALSE;
        }
        else {
            return TRUE;
        }

    }

    public function disconnect() {
        mysql_close($this->db);
    }

    public function select_db($dbName) {
        mysql_select_db($dbName);
    }

    public function query($qString) {
        $q = mysql_query($qString);
        return $q;
    }

    public function fetch_assoc($qResult) {
        $r = array();
        $r = mysql_fetch_assoc($qResult);
        return $r;
    }
    
    public function fetch_row($qResult) {
        $r = array();
        $r = mysql_fetch_row($qResult);
        return $r;
    }
    
    public function num_rows($qResult) {
        return mysql_num_rows($qResult);
    }

}

class metadb extends mysql_db_driver {
    
    function getUserDataBySID($sid) {
        $r = $this->query("SELECT uid FROM mcp_session WHERE sid='$sid'");
        if($this->num_rows($r)==0) {
            return FALSE;
        }
        $id = $this->fetch_assoc($r);
        return $this->getUserDataByID($id['uid']);
    }

    function getUserDataByID($id) {
        $r = $this->query("SELECT * FROM mcp_users WHERE id=$id");
        if($this->num_rows($r)==0) {
            return FALSE;
        }
        $data = $this->fetch_assoc($r);
        return $data;
    }

    function getUserID($login) {
        $r = $this->query("SELECT id FROM mcp_users WHERE login='$login'");
        if($this->num_rows($r)==0) {
            return FALSE;
        }
        $data = $this->fetch_assoc($r);
        return $data['id'];
    }

    function getUserIDByCode($code) {
        $r = $this->query("SELECT uid FROM mcp_regtemp WHERE act_key='$code'");
        if($this->num_rows($r)==0) {
            return FALSE;
        }
        $data = $this->fetch_assoc($r);
        return $data['uid'];
    }

    function getUserDataByLogin($login) {
        $r = $this->query("SELECT * FROM mcp_users WHERE login='$login'");
        if($this->num_rows($r) == 1) {
            $data = $this->fetch_assoc($r);
            return $data;
        }
        return FALSE;
    }

    function CheckLogin($login) {
        $r = $this->query("SELECT id FROM mcp_users WHERE login='$login'");
        if($this->num_rows($r)!=0) {
            return FALSE;
        }
        return TRUE;
    }

    function CheckMail($mail) {
        $r = $this->query("SELECT id FROM mcp_users WHERE email='$mail'");
        if($this->num_rows($r)!=0) {
            return FALSE;
        }
        return TRUE;
    }

    function CheckSession($sess_id) {
        return FALSE;
    }

    function CreateUser($login,$pass,$mail,$salt,$date) {
        $this->query("INSERT INTO mcp_users VALUES(0,0,'$login','$pass','$salt','$mail',$date,$date
                        ,'{$_SERVER['REMOTE_ADDR']}','{$_SERVER['REMOTE_ADDR']}')");

    }

    function addActivationCode($code,$login) {
        $date = time();
        $uid = $this->getUserID($login);
        $ip = $_SERVER['REMOTE_ADDR'];
        $this->query("INSERT INTO mcp_regtemp VALUES('$code',$uid,$date,'$ip')");
    }

    function ChangeUserAccess($id,$access) {
        $this->query("UPDATE mcp_users SET access=$access WHERE id=$id");
    }

    function ActivateUser($id,$code) {
        $this->query("UPDATE mcp_users SET access=1 WHERE id=$id");
        $this->query("DELETE FROM mcp_regtemp WHERE act_key='$code'");
    }

    function sessionRegister($session_id,$uid){
        $dateline = time();
        $this->query('INSERT INTO mcp_session VALUES(\''.$session_id.'\','.$uid.',\''.$dateline.'\',\''.$_SERVER['REMOTE_ADDR'].'\')');
     }

    function sessionUnRegister($session_id){
        $this->query('DELETE FROM mcp_session WHERE sid = \''.$session_id.'\'');
    }
}

?>
